<?php
namespace App\Services;

use App\Helpers\Api\MiniProgramTrait;
use App\Models\Subscribe;

class PublishTemplateService {
    use MiniProgramTrait;
    private $openid;

    private function _send($data) {
        $app = $this->miniProgram();
        if (empty($data['touser']))
            return;
        \Log::info(var_export($data, true));
        $list = $app->subscribe_message->send($data);
        if ($list['errcode'] != 0)
            \Log::error(json_encode_cn($data) . " response: " . $list['errmsg']);
        \Log::info(var_export($list, true));
    }

    public function comment($openid, $message, $nickname, $article_id, $type) {
        if ($type == 2)
            $page = '/package-home/pages/videoDetail/index?id=' . $article_id . '&openType=share';
        else
            $page = '/package-home/pages/dynamicDetail/index?id=' . $article_id;
        $data = [
            'template_id' => Subscribe::TEMPLATE_COMMENT,
            'touser' => $openid,
            'page' => $page,
            'data' => [
                'thing4' => [
                    'value' => '可以在设置中关闭本文的消息提醒'
                ],
                'thing5' => [
                    'value' => $nickname,
                ],
                'thing6' => [ //动态内容
                    'value' => $message ? mb_substr($message, 0, 20): '一条有趣有爱的内容'
                ],
                'time3' => [
                    'value' => date('Y-m-d H:i:s'),
                ]
            ],
        ];
        $this->_send($data);
    }


    public function articleStar($openid, $title, $article_id, $type) {
        if ($type == 2)
            $page = '/package-home/pages/videoDetail/index?id=' . $article_id . '&openType=share';
        else
            $page = '/package-home/pages/dynamicDetail/index?id=' . $article_id;
        $data = [
            'template_id' => Subscribe::TEMPLATE_ARTICLE_STAR,
            'touser' => $openid,
            'page' => $page,
            'data' => [
                'thing8' => [
                    'value' => $title ? mb_substr($title, 0, 10) : '一条有趣有爱的内容',
                ],
                'date2' => [
                    'value' => date('Y-m-d H:i:s'),
                ],
                'thing5' => [
                    'value' => '这个动态很受欢迎哦，快来看看谁给你点赞吧'
                ]
            ],
        ];
        $this->_send($data);
    }

    public function circleArticleNotice($openid, $circle_title, $circle_num, $article_num, $circle_id, $is_activity = 0) {
        if ($article_num > 99) {
            $article_num = '99+';
        }
        $circle_title = removeEmoji($circle_title);
        if ($circle_num > 1) {
            $msg = mb_substr($circle_title . '等' .$circle_num.'个圈子', 0, 20);
        } else {
            $msg = mb_substr($circle_title, 0, 20);
        }
        if ($is_activity) {
            $page = '/package-circle/pages/operateCircleDetail/index?id='.$circle_id.'&openType=share';
        } else {
            $page = '/package-circle/pages/circleDetail/index?id=' . $circle_id. '&openType=share';
        }
        $data = [
            'template_id' => Subscribe::TEMPLATE_CIRCLE_ARTICLE_NOTICE,
            'touser' => $openid,
            'page' => $page,
            'data' => [
                'thing1' => [
                    'value' => $msg,
                ],
                'thing4' => [
                    'value' => '更新了'.$article_num.'条动态',
                ],
            ],
        ];
        $this->_send($data);
    }

    public function signIn($openid, $series_day) {
        $data = [
            'template_id' => Subscribe::TEMPLATE_SIGN_IN,
            'touser' => $openid,
            'page' => '/pages/my/index',
            'data' => [
                'thing4' => [
                    'value' => '您已经连续签到了'.$series_day.'天，请继续加油',
                ],
                'time8' => [
                    'value' => date('Y-m-d H:i:s')
                ],
            ],
        ];
        $this->_send($data);
    }

    public function birthday($openid, $title) {
        $data = [
            'template_id' => Subscribe::TEMPLATE_BIRTHDAY,
            'touser' => $openid,
            'page' => '/pages/index/index',
            'data' => [
                'thing1' => [
                    'value' => $title . '生日',
                ],
                'thing2' => [
                    'value' => $title . '今天生日，别忘记给TA祝福哦'
                ],
            ],
        ];
        $this->_send($data);
    }

    public function activity_start($openid, $title, $circle_id, $start_at, $end_at) {
        $data = [
            'template_id' => Subscribe::TEMPLATE_ACTIVITY_START,
            'touser' => $openid,
            'page' => '/package-circle/pages/operateCircleDetail/index?id=' . $circle_id. '&openType=share',
            'data' => [
                'thing1' => [
                    'value' => mb_substr($title, 0, 20),
                ],
                'date5' => [
                    'value' => $start_at,
                ],
                'date6' => [
                    'value' => $end_at,
                ],
                'thing7' => [
                    'value' => mb_substr('参与活动：'.$title.'，赢取奖品', 0, 20),
                ],
            ],
        ];
        $this->_send($data);
    }


    public function activity_end($openid, $title, $circle_id) {
        $data = [
            'template_id' => Subscribe::TEMPLATE_ACTIVITY_END,
            'touser' => $openid,
            'page' => '/package-circle/pages/operateCircleDetail/index?id=' . $circle_id. '&openType=share',
            'data' => [
                'thing1' => [
                    'value' => $title,
                ],
                'thing2' => [
                    'value' => '可前往查看自己是否中奖',
                ],
            ],
        ];
        $this->_send($data);
    }


    public function activity_countdown($openid, $title, $circle_id, $rank) {
        $data = [
            'template_id' => Subscribe::TEMPLATE_ACTIVITY_COUNT_DOWN,
            'touser' => $openid,
            'page' => '/package-circle/pages/operateCircleDetail/index?id=' . $circle_id. '&openType=share',
            'data' => [
                'thing1' => [
                    'value' => mb_substr($title, 0, 20),
                ],
                'thing3' => [
                    'value' => '第' . $rank . '名', //rank
                ],
                'thing4' => [
                    'value' => '活动还剩最后1天，大奖就在眼前',
                ],
            ],
        ];
        $this->_send($data);
    }

    public function order_deliver($openid, $title, $express_title, $express_sn, $date) {
        $data = [
            'template_id' => Subscribe::TEMPLATE_ORDER_DELIVER,
            'touser' => $openid,
            'page' => '/package-exchange/pages/history/index',
            'data' => [
                'name1' => [
                    'value' => mb_substr($title, 0, 20),
                ],
                'thing7' => [
                    'value' => $express_title,
                ],
                'character_string8' => [
                    'value' => $express_sn,
                ],
                'date6' => [
                    'value' => $date
                ],
            ],
        ];
        $this->_send($data);
    }

}
